// tsc .\tscode.ts  tab补全
//新建终端：ctrl shift ·
// 布尔
var flag: boolean = true;
// 数字
var age: number = 18;
// 字符串
var msg: string = "hyt";
// Array
var arr1: number[] = [1, 2, 3];
var arr2: Array<number> = [1, 2, 3];

// 枚举：变量的值是有限的，比如性别
enum Gender {
  MAN,
  WOMAN,
}
console.log(Gender);
console.log(Gender["MAN"]);
console.log(Gender["WOMAN"]);

enum Direct {
  N = "north",
  S = "south",
  W = "west",
  E = "east",
}
console.log(Direct["N"]);
console.log(Direct["S"]);
console.log(Direct["W"]);
console.log(Direct["E"]);

// 元组
var man: [string, number, string, string[]] = [
  "何玉婷",
  18,
  "女",
  ["钱", "帅哥"],
];

// void空白代表没有任何类型 常用于返回值
function fn(): void {}

// never是异常的数据类型，unknow是any的安全类型
var value: unknown; //可以把unknow类型的类型赋任何值
value = 1;

// 字面量类型
type EventType = "click" | "dbclick";
var ev: EventType = "click";
document.addEventListener(ev, function () {
  console.log(666);
});

// 联合类型
function getSum(a: number | string, b: number | string): number {
  return +a + +b;
}
console.log(getSum(1, 2));
console.log(getSum("1", "2"));

// 交叉类型
interface IUser {
  id: number;
  name: string;
}
interface IInfo {
  score: number;
}
const user1: IUser & IInfo = {
  id: 1,
  name: "何玉婷",
  score: 99,
};

// 可选类型
interface IStudent {
  id: number;
  name: string;
  score: number;
}
var s1: Partial<IStudent> = {
  id: 0,
  name: "何玉婷",
};

// 条件类型：三元表达式
interface Fish {
  name1: string;
}
interface Water {
  name2: string;
}
interface Bird {
  name3: string;
}
interface Sky {
  name4: string;
}
type TAnimal<T> = T extends Fish ? Water : Sky;
var animal: TAnimal<Bird> = {
  name4: "天空",
};
